function gadget:GetInfo()
	return {
		name = "Rampage Ability",	-- this whole section is just fake anyhow... but nice to have the info in a coherent manner
		desc = "Increases Attack speed (and should increase speed) over a short time",
		author = "Sean Heron",
		date = "January 2009",
		license = "GNU GPL v2, or later",
		layer = 26, -- watchout for which layer! -- lowering layer to the benefit of Knight initialising its global var rampageLvl
		enabled = true
	}
end

local debugon = false

local knight=UnitDefNames.knight.id -- want to make independent of this really - be called from Bishop.lua (same is valid for Jump!!)

local rampageUnits= {
	[knight]=true,
}

local listOfRampageUnits = {}

-- rampage related vars

local rampageEndTime = {}
local rampageingUnit = {}
local betterWeapon = {}

local CMD_RAMPAGE = 32299


if (gadgetHandler:IsSyncedCode()) then

--SYNCED


local rampageDesc = {
	name="Rampage",
	tooltip="higher rate of attack, and speed increase",
	action="rampage",
	id=CMD_RAMPAGE,
	type=CMDTYPE.ICON,
}


function gadget:Initialize()
		
end

function gadget:UnitCreated(unitID, unitDefID, teamID)
	if rampageUnits[unitDefID] then	-- rampageUnits should go to config file?
		rampageEndTime[teamID] = 0 -- so that - oh. this is still not for every knight... it's for all knights per team ...

		-- add unitID to listOfRampageUnits
		listOfRampageUnits[unitID] = true

	end
end

function gadget:UnitDestroyed(unitID, unitDefID, teamID, attacker)
	if rampageUnits[unitDefID] then	-- reset rampageTimer for units destroyed on rampage
		rampageEndTime[teamID] = 0	-- this needs to change - switch to using unitIDs ?
		-- remove UnitID from listOfRampageUnits
		listOfRampageUnits[unitID] = nil	-- hmm, this table is going to grow over time - maybe remove the entry at position unitID? yah, probably better
				-- see problem noted above - but at least it works...
		-- table.remove (listOfRampageUnits, unitID)	-- question is whether this removes the "unitID'th" entry, or the entry with index number "unitID"...
		-- doesn't seem to work the way I'd want it to.
		-- can I check for which position a given index is, and then use that ? should go, I'd think.. not found that function yet..
		end

end


function gadget:GameFrame(f)

	-- every x (currently 30) frames - do this. Prob want to spread over different gadgets, ie to step (do same size step, but not "land" at same frame with all)
	if f%30 < .1 then -- every second, ie every 30 frames? yes. Probably want to set this at a higher refresh rate though - either every frame or every 5 or 6
		--Spring.Echo("come on !")
		for unitID,_ in pairs(listOfRampageUnits) do
			--if listOfRampageUnits == true then
				local teamID = Spring.GetUnitTeam(unitID)	-- is the teamID wrong ? no it was the table not being cleaned up proper
				-- okay - lets use a trick till we switch to a coherent system: we use listOfRampageUnits, and then just get the Units team, and use that as index value like we have been
						-- then as said - when we sort out a more universal system for tracking / managing Engines / upgradable units, we can fix it proper

				-- end Rampage - well, check if we need to end
				if rampageEndTime[teamID]~=0 and Spring.GetGameSeconds() >=  rampageEndTime[teamID] then	-- teamID is a problem in GameFrame - just iterate through the pairs ? Or add another table that says which Knights are actually on rampage... (hmm - but hmm)
					-- set weapon back to its earlier values

					betterWeapon = {		-- could make a "betterWeapon" table, and just set to betterWeapon[1], etc..
						reloadTime  = 3, -- 3
					--	sprayAngle  = 800, --800
					--	range       = 1200, --400
						burst = 4, -- 4
					--	burstRate = .3, -- .3
					}
					Spring.SetUnitWeaponState ( rampageingUnit[teamID], 1, betterWeapon ) 	-- we were missing the team ID of course..
												-- unitID ?? where to get from... - or save it when we set rampagetimer...
												-- maybe steal Zwzsgs "gettheone()"...
					Spring.Echo("Rampage over!")
					rampageEndTime[teamID] = 0
					-- then just need to implement cooldown
				end
			--end	think I forgot to remove this "end" - no wonder there was trouble...
		end


	end



end


function gadget:CommandFallback(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions, cmdTag)


	if cmdID == CMD_RAMPAGE then

		if debugon == true then
			local qreloadTime = Spring.GetUnitWeaponState( unitID, 1 , "reloadTime")
			Spring.Echo("Pre rampage reloadTime: ")
			Spring.Echo(qreloadTime)
		end


		local rampageLvl =  _G.AllEngines:GetEngineByUnitID(unitID):GetAbilityLvl(1)
		--- Rampage stuff (in transition to seperate gadget :P )
		if rampageEndTime[teamID] == 0 then
			-- just a slight increase in attack speed - works out to x 1.125 for now
			-- if debugon == true then
			Spring.Echo("Rampage being called")
			Spring.Echo("Rampage level at".. rampageLvl)
			-- end
			if rampageLvl == 1 then
				betterWeapon = {		-- need to set this up for just a certain amount of time.. - plus how to best take into account changes from elsewhere... ?
					reloadTime  = 2, -- 3		-- probably not going to work like this... it did actually!!
				--	sprayAngle  = 800, --800		-- is it a problem if I didn't set all of these ? probably yes...
				--	range       = 1200, --400		-- actually - no!
					burst = 3, -- 4 (not three ?) - no was 4
				--	burstRate = .3, -- .3
				}
				rampageEndTime[teamID] = Spring.GetGameSeconds() + 10 -- or such - check against it in Gameframe

			end
			if rampageLvl == 2 then
				betterWeapon = {		-- need to set this up for just a certain amount of time.. - plus how to best take into account changes from elsewhere... ?
					reloadTime  = 1, -- 3		-- probably not going to work like this... it did actually!!
				--	sprayAngle  = 800, --800		-- is it a problem if I didn't set all of these ? probably yes...
				--	range       = 1200, --400		-- actually - no!
					burst = 2, -- 4 (not three ?) - no was 4
				--	burstRate = .3, -- .3
				}
				rampageEndTime[teamID] = Spring.GetGameSeconds() + 10 -- same as above

			end
			if rampageLvl == 3 then
				betterWeapon = {		-- need to set this up for just a certain amount of time.. - plus how to best take into account changes from elsewhere... ?
					reloadTime  = 1, -- 3		-- probably not going to work like this... it did actually!!
				--	sprayAngle  = 800, --800		-- is it a problem if I didn't set all of these ? probably yes...
				--	range       = 1200, --400		-- actually - no!
					burst = 2, -- 4 (not three ?) - no was 4
				--	burstRate = .3, -- .3
				}
				rampageEndTime[teamID] = Spring.GetGameSeconds() + 15 -- longer rampage

			end
			Spring.SetUnitWeaponState ( unitID, 1, betterWeapon ) 	-- huh, what's the "1" ? -- oh, it's which weapon. Was this starting counting at "0", wasn't it. The joker... :/

			if debugon == true then
				local qreloadTime = Spring.GetUnitWeaponState( unitID, 1 , "reloadTime")
				Spring.Echo("Rampage lowered reloadTime: ")
				Spring.Echo(qreloadTime)
			end

			rampageEndTime[teamID] = Spring.GetGameSeconds() + 10 -- or such - check against it in Gameframe
			rampageingUnit[teamID] = unitID
			-- Spring.Echo(rampageEndTime[teamID])

			return true, true

		end
	end
	return false
end




else
--UNSYNCED


function gadget:Update()

end

end